import {createRouter,createWebHistory} from 'vue-router'

import {loginRoutes}  from '../views/Login/login'
import { routes } from './routes'

const router = createRouter({
    history:createWebHistory(),
    routes:[
        {
            path:'/',
            redirect:'/login'
        },
        {
            path:'/main',
            name:'main',
            meta:{title:'首页'},
            component:() => import('../views/Main/Main.vue'),
            children:[...routes]
        },
        ...loginRoutes,
        {
            path:'/503',
            name:'503',
            component:() => import('../views/ErrorPage/503.vue')
        },
        {
            path:'/404',
            name:'404',
            component:() => import('../views/ErrorPage/404.vue')
        },
        {
            // 路由不存在，跳转404界面
            path:'/:pathMatch(.*)',
            redirect:'/404'
        }
    ]
})


// 全局导航守卫
router.beforeEach((to,from) => {
    if(to.path.startsWith('/main')) {
        if(!localStorage.getItem('token')) {
            router.push('/login')
            return false
        }
        return true
    }
})

export default router